DWN
DWN Client
1. Install
npm i @extrimian/dwn-client
2. Init DWN Client
When we wants to intance a DWN it has to be config passing a DID, to pull messages from that DID, inboxURL that is the endpoint of our DWN service, and a storage to save messages.
const dwnClient = new DWNClient({
did : "did:example";
inboxURL : "dwnURL";
storage : MessageStorage;
});
For this test case you can create a storage that saves the data in memory.
export const storageMock: MessageStorage = {
async getMessages(): Promise<Entry[]> {
return messagesStorage;
},
async getLastPullDate(): Promise<Date> {
return lastPullDate;
},
async saveMessages(messages: Entry[]): Promise<void> {
messagesStorage.push(...messages);
},
async updateLastPullDate(date: Date): Promise<void> {
lastPullDate = date;
},
};
3. Send a message to the DWN
const sendMsg = await dwnClient.sendMessage({
targetDID: "did:example", //Recipient's DID
targetInboxURL: "dwnURL", // Recipient's DWN service, obtained by resolving the DID Document
message: {
data: packedMessage, //message packed with Didcomm
descriptor: {
method: ThreadMethod.Create,
dateCreated: new Date(),
dataFormat: "application/json",
},
},
});
DWN Types
The message structure is already defined in the protocol so would be as the following.
export type Entry = {
descriptor: MessageDescriptor;
data?: any;
};
export type MessageDescriptor = {
method: ThreadMethod;
// UUIDv4
objectId?: string;
// Content-Type header (e.g. application/json)
dataFormat?: string;
// content id
cid?: {
codec: string;
version: number;
hash: any;
};
// Creation date in UNIX timestamp format
dateCreated?: number;
// Data Schema URL
schema?: string;
// Initial thread message id
root?: string;
// Parent (replying to) message id
parent?: string;
};
DWN Scheduler to pull messages
1. Install
npm i @extrimian/dwn-client-scheduler
2. Init DwnClientScheduler
const dwnClientScheduler = new DWNClientScheduler(dwnClient, cronExpression);
dwnClientScheduler.start();
dwnClient.addSubscriber(async (messages) => {
console.log(`Found ${messages.length} messages`);
console.log(messages);
});
const cronExpression = "*/10 * * * * *";